﻿#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
namespace str
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;
		//迭代器
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}
		const_iterator begin() const
		{
			return _str;
		}
		const_iterator end() const
		{
			return _str + _size;
		}
		string(const char*str=" ")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity + 1];

			strcpy(_str, str);
		}
		string(const string& s)//深拷贝
		{
			_str = new char[_capacity + 1];
			strcpy(_str, s._str);
			_size = s._size;
			_capacity = s._capacity;
		}
		string& operator=(const string &s)
		{
			if (this != &s)
			{
				char* tmp = new char[_capacity + 1];
				strcpy(tmp, s._str);
				delete[] _str;
				_str = tmp;
				_size = s._size;
				_capacity = s._capacity;
			}
			return *this;
		}

		const char* c_str()	const
		{
			return _str;
		}
		size_t size()	const
		{
			return _size;

		}
		//可理解为realloc
		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n+1];//开辟新空间,注意多开辟一个空间给\0
				strcpy(tmp, _str);
				//释放原来空间
				delete[] _str;
				//将老空间的值赋给新空间
				_str = tmp;
				_capacity = n;
			}
		}
		void push_back(char ch)//字符
		{
			if (_size == _capacity)
			{
				size_t newCapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newCapacity);
			}

			_str[_size] = ch;
			_size++;
			//保证最后一个为\0
			_str[_size] = '\0';
		}
	//字符串
		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}

			strcpy(_str + _size, str);
			_size += len;
		}
		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}
		string& operator+=(char ch)
		{
			push_back(ch);

			return *this;
		}

		string& operator+=(const char* str)
		{
			append(str);

			return *this;
		}
		void insert(size_t pos, char ch)
		{
			assert(pos <= _size);

			if (_size == _capacity)
			{
				size_t newCapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newCapacity);
			}
			size_t end = _size + 1;
			while (end > pos)
			{
				//移位
				_str[end] = _str[end - 1];
				--end;
			}

			_str[pos] = ch;
			_size++;
		}

		void insert(size_t pos, const char* str)
		{
			assert(pos <= _size);
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}

			int end = _size;
			while (end >= (int)pos)
			{
				_str[end + len] = _str[end];
				--end;
			}

			strncpy(_str + pos, str, len);
			_size += len;
		}
		
		void erase(size_t pos, size_t len = npos)
		{
			assert(pos < _size);
			if (len==npos||pos + len > _size)
			{
				//直接覆盖,后_size也跟着变
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				strcpy(_str + pos, _str + pos + len);
				_size -=len;
			}		
		}
		void swap(string& s)
		{
			//交换函数，直接改变指向
			//利用std中的swap
			std::swap(_str, s._str);
		}
		size_t find(char ch, size_t pos = 0)
		{
			for (int i = pos; i < _size; i++)
			{
				if (_str[i] == ch)
				{
					return i;
				}				
			}
			return npos;
		}
		size_t find(char* str,size_t pos=0)
		{
			const char* ptr = strstr(_str+pos, str);
			if (ptr == nullptr)
			{
				return npos;
			}
			else
			{
				return ptr-_str;//指针减指针
			}
		}
		string substr(size_t pos = 0, size_t len = npos)
		{
			assert(pos < _size);
			size_t end = pos + len;
			if (len == npos || pos + len >= _size)
			{
				end = _size;
			}

			string str;
			str.reserve(end - pos);
			for (size_t i = pos; i < end; i++)
			{
				str += _str[i];
			}

			return str;
		}
		~string()
		{
			_size = 0;
			_capacity = 0;
			delete[] _str;
			_str = nullptr;
		}
	private:
		size_t _size;
		size_t _capacity;
		char* _str;		
		const static size_t npos = -1;
	};

} 
int main()
{
	str::string s1("hello world");
	str::string s2("shuaiwei");
	//string s3;
	//s1.swap(s2);
	//cout << s1.c_str() << endl;
	//cout << s2.c_str() << endl;
	//s1.erase(3);
	//cout << s1.c_str() << endl;
	s1.append("sssssw");
	//str::string s3 = s1.substr(2, 4);
	
	
	//cout << s2.c_str() << endl;
	cout << s2.begin() << endl;

	return 0;
}

